package com.longnuoqi.mydoctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import com.longnuoqi.mydoctor.utils.ActivityCollection;
import com.longnuoqi.mydoctor.utils.ViewPagerUtil;
import com.yunhe.mydoctor.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Lenovo on 2015/12/25.
 */
public class WelcomeActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private ImageView intoBtn;
    private final static int[] pics = {R.mipmap.yindaoye1, R.mipmap.yindaoye2, R.mipmap.yindaoye3}; //引导页面图片
    private static boolean isExit = false;//判断是否退出
    private static boolean hasTask = false;//判断是否是任务
    private Timer timer = new Timer();//声明timer对象，计时器
    private TimerTask timerTask = new TimerTask() {//为timer安排一次执行或重复执行的任务
        @Override
        public void run() {
            isExit = false;
        }
    };

    /*双击退出程序*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(WelcomeActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                if (!hasTask) {
                    timer.schedule(timerTask, 2000);//安排在指定延迟后执行指定的任务
                }
            } else {
                ActivityCollection.finishAll();
                //回收资源
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_layout);
        ActivityCollection.addActivity(this);
        init();
    }

    private void init() {
        intoBtn = (ImageView) findViewById(R.id.into_btn_of_viewpager);
        intoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        new ViewPagerUtil().viewPagerUtil(WelcomeActivity.this, getWindow().getDecorView(), viewPager, pics);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollection.removeActivity(this);
    }

}
